package com.spic.business.informatization.accounting.service.impl;

import java.util.Collections;
import java.util.List;

import com.spic.business.informatization.meeting.domain.MeetingActivity;
import com.spic.business.two.tickets.general.eum.TableCategoryEnum;
import com.spic.business.two.tickets.general.service.ITicketGeneratorService;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.stream.Collectors;

import org.springframework.transaction.annotation.Transactional;
import com.spic.business.informatization.accounting.domain.MeetingWorkingGroup;
import com.spic.business.informatization.accounting.mapper.MeetingAccountingMapper;
import com.spic.business.informatization.accounting.domain.MeetingAccounting;
import com.spic.business.informatization.accounting.service.IMeetingAccountingService;
import org.springframework.util.CollectionUtils;

import static java.util.stream.Collectors.toList;

/**
 * 班前班后会Service业务层处理
 *
 * @author spic
 * @date 2021-11-04
 */
@Service
public class MeetingAccountingServiceImpl implements IMeetingAccountingService
{
    @Autowired
    private MeetingAccountingMapper meetingAccountingMapper;
    @Autowired
    private ITicketGeneratorService ticketGeneratorService;
    /**
     * 查询班前班后会
     *
     * @param id 班前班后会主键
     * @return 班前班后会
     */
    @Override
    public MeetingAccounting selectMeetingAccountingById(Long id)
    {
        return meetingAccountingMapper.selectMeetingAccountingById(id);
    }

    /**
     * 查询班前班后会列表
     *
     * @param meetingAccounting 班前班后会
     * @return 班前班后会
     */
    @Override
    public List<MeetingAccounting> selectMeetingAccountingList(MeetingAccounting meetingAccounting)
    {
        return meetingAccountingMapper.selectMeetingAccountingList(meetingAccounting);
    }

    /**
     * 新增班前班后会
     *
     * @param meetingAccounting 班前班后会
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMeetingAccounting(MeetingAccounting meetingAccounting)
    {
        int rows = meetingAccountingMapper.insertMeetingAccounting(meetingAccounting);
        insertMeetingWorkingGroup(meetingAccounting);
        return rows;
    }

    /**
     * 修改班前班后会
     *
     * @param meetingAccounting 班前班后会
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMeetingAccounting(MeetingAccounting meetingAccounting)
    {
        meetingAccounting.setUpdateTime(DateUtils.getNowDate());
        meetingAccountingMapper.deleteMeetingWorkingGroupByMeetingAccountingId(meetingAccounting.getId());
        insertMeetingWorkingGroup(meetingAccounting);
        return meetingAccountingMapper.updateMeetingAccounting(meetingAccounting);
    }

    /**
     * 批量删除班前班后会
     *
     * @param ids 需要删除的班前班后会主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMeetingAccountingByIds(Long[] ids)
    {
        meetingAccountingMapper.deleteMeetingWorkingGroupByMeetingAccountingIds(ids);
        return meetingAccountingMapper.deleteMeetingAccountingByIds(ids);
    }

    /**
     * 删除班前班后会信息
     *
     * @param id 班前班后会主键
     * @return 结果
     */
    @Override
    public int deleteMeetingAccountingById(Long id)
    {
        meetingAccountingMapper.deleteMeetingWorkingGroupByMeetingAccountingId(id);
        return meetingAccountingMapper.deleteMeetingAccountingById(id);
    }

    @Override
    public void generateTicket(Long id) {
        MeetingAccounting meetingAccounting = meetingAccountingMapper.selectMeetingAccountingById(id);
        if(CollectionUtils.isEmpty(meetingAccounting.getMeetingWorkingGroupList())) return;
        List<MeetingWorkingGroup> groups = meetingAccounting.getMeetingWorkingGroupList().stream()
                .filter(MeetingWorkingGroup :: needTicket).collect(toList());
        compose(groups);
    }
    private void compose(List<MeetingWorkingGroup> groups) {
        for(MeetingWorkingGroup group:groups){
            TableCategoryEnum tableCategoryEnum = TableCategoryEnum.valueOf(group.getTicketType());
            Long id = ticketGeneratorService.createTickets(tableCategoryEnum,group);
            group.setTicketId(id);
            meetingAccountingMapper.updateMeetingGroup(group);
        }

    }
    /**
     * 新增班前班后会工作组信息
     *
     * @param meetingAccounting 班前班后会对象
     */
    public void insertMeetingWorkingGroup(MeetingAccounting meetingAccounting)
    {
        List<MeetingWorkingGroup> meetingWorkingGroupList = meetingAccounting.getMeetingWorkingGroupList();
        Long id = meetingAccounting.getId();
        if (StringUtils.isNotNull(meetingWorkingGroupList))
        {
            List<MeetingWorkingGroup> list = new ArrayList<MeetingWorkingGroup>();
            for (MeetingWorkingGroup meetingWorkingGroup : meetingWorkingGroupList)
            {
                meetingWorkingGroup.setMeetingAccountingId(id);
                list.add(meetingWorkingGroup);
            }
            if (list.size() > 0)
            {
                meetingAccountingMapper.batchMeetingWorkingGroup(list);
            }
        }
    }
}
